package com.infra.micro.quartz.domain.repository;

import com.infra.micro.quartz.domain.entity.QuartzJobExample;
import com.infra.micro.quartz.domain.entity.QuartzJobPO;
import com.infra.micro.quartz.domain.mapper.QuartzJobMapper;
import com.infra.micro.quartz.model.req.QueryQuartzJobReq;
import com.infra.micro.quartz.model.vo.QuartzJobVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class QuartzJobRepository {
    @Resource
    private QuartzJobMapper quartzJobMapper;

    public List<QuartzJobPO> queryList(boolean isPause) {
        QuartzJobExample example = new QuartzJobExample();
        example.createCriteria().andIsPauseEqualTo(isPause);
        return quartzJobMapper.selectByExample(example);
    }

    /**
     * 创建
     *
     * @param quartzJobVO
     */
    public boolean insert(QuartzJobVO quartzJobVO) {
        QuartzJobPO record = new QuartzJobPO();
        BeanUtils.copyProperties(quartzJobVO, record);
        boolean ret = quartzJobMapper.insertSelective(record) > 0;
        quartzJobVO.setJobId(record.getJobId());
        return ret;
    }

    /**
     * 编辑
     *
     * @param quartzJobVO
     */
    public boolean update(QuartzJobVO quartzJobVO) {
        QuartzJobPO record = new QuartzJobPO();
        BeanUtils.copyProperties(quartzJobVO, record);
        return quartzJobMapper.updateByPrimaryKeySelective(record) > 0;
    }

    /**
     * 删除任务
     *
     * @param jobIds
     */
    public int batchDeleteByIds(List<Long> jobIds) {
        QuartzJobExample example = new QuartzJobExample();
        example.createCriteria().andJobIdIn(jobIds);
        return quartzJobMapper.deleteByExample(example);
    }

    /**
     * 通过jobId更新暂停状态
     *
     * @param jobId
     * @return
     */
    public boolean updateIsPauseById(Long jobId, boolean isPause) {
        QuartzJobPO record = new QuartzJobPO();
        record.setJobId(jobId);
        record.setIsPause(isPause);
        return quartzJobMapper.updateByPrimaryKeySelective(record) > 0;
    }

    /**
     * 根据ID查询
     *
     * @param jobId ID
     * @return
     */
    public QuartzJobPO queryById(Long jobId) {
        return quartzJobMapper.selectByPrimaryKey(jobId);
    }

    /**
     * 通过jobId删除
     *
     * @param jobId
     * @return
     */
    public boolean deleteById(Long jobId) {
        return quartzJobMapper.deleteByPrimaryKey(jobId) > 0;
    }

    /**
     * 分页查询
     *
     * @param req
     * @return
     */
    public List<QuartzJobPO> queryListByPage(QueryQuartzJobReq req) {
        QuartzJobExample example = new QuartzJobExample();
        QuartzJobExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(req.getJobName())) {
            criteria.andJobNameLike("%" + req.getJobName() + "%");
        }

        if (req.getPause() != null) {
            criteria.andIsPauseEqualTo(req.getPause());
        }

        return quartzJobMapper.selectByExample(example);
    }
}
